package com.woniu.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniu.exception.LXLGException;
import com.woniu.user.common.AddressException;
import com.woniu.user.common.UserException;
import com.woniu.user.infra.model.Address;
import com.woniu.user.infra.mapper.AddressMapper;
import com.woniu.user.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.user.service.CityService;
import com.woniu.user.service.dto.AddressDto;
import com.woniu.user.service.dto.CityDto;
import com.woniu.user.service.param.InsertAddressServiceParam;
import com.woniu.user.service.param.UpdateServiceAddress;
import com.woniu.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 地址表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-11-26
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {


    /**
     * 查询用户地址
     *
     * @return
     */
    @Override
    public List<AddressDto> listUserAddress(Integer userId, Integer addressId) {
        List<Address> address = list(new QueryWrapper<Address>().eq("user_id", userId));
        if (address.isEmpty()) throw new LXLGException(UserException.ACCOUNT_NOT_EXIST);
        List<AddressDto> addressDtos = null;
        if (addressId == -1) {
            //不穿参数查看用户个人所有收获地址
            List<Address> allAddress = list(new QueryWrapper<Address>().eq("user_id", userId).eq("is_delete", 0));
            addressDtos = BeanUtil.copyToList(allAddress, AddressDto.class);
        } else if (userId != null && addressId != -1) {
            //购买下单确认收获地址
            List<Address> allAddress = list(new QueryWrapper<Address>().eq("user_id", userId).eq("is_delete", 0).eq("address_id", addressId));
            addressDtos = BeanUtil.copyToList(allAddress, AddressDto.class);
        }

        return addressDtos;
    }

    /**
     * 添加地址
     *
     * @param insertAddressServiceParam
     * @return
     */
    @Override
    public boolean insertAddress(InsertAddressServiceParam insertAddressServiceParam) {

        Address address = BeanUtil.copyProperties(insertAddressServiceParam, Address.class);

//        address.setUserId(insertAddressServiceParam.getUserId());
        address.setModifiedTime(DateUtil.parseDateLong());
        address.setIsDefault("NO");
        address.setIsDelete(0);
        save(address);
        return true;
    }

    /**
     * 设为默认地址
     *
     * @param addressId
     * @return
     */
    @Override
    public boolean defultAddress(Integer addressId) {
        //TODO 当前已是默认地址，抛异常
        //查询地址
        Address address = getOne(new QueryWrapper<Address>().eq("address_id", addressId).eq("is_delete", 0));
        if (address == null) throw new LXLGException(AddressException.ADDRESS_NOT_EXIST);
        //取消原来的默认地址
        Address one = getOne(new QueryWrapper<Address>().eq("is_default", "YES"));
        one.setIsDefault("NO");
        updateById(one);

        address.setIsDefault("YES");
        updateById(address);
        return true;
    }

    /**
     * 修改地址
     *
     * @param updateAddress
     * @return
     */
    @Override
    public boolean updateAddress(UpdateServiceAddress updateAddress) {
        Address address = BeanUtil.copyProperties(updateAddress, Address.class);
        address.setModifiedTime(DateUtil.parseDateLong());
        updateById(address);

        return true;
    }

    /**
     * 删除
     *
     * @param addressId
     * @return
     */
    @Override
    public boolean deleteAddress(Integer addressId) {
        //TODO 删除的是默认地址该怎么办
        System.out.println(addressId);
        //查询地址
        Address address = getOne(new QueryWrapper<Address>().eq("address_id", addressId));
        System.out.println(address);
        if (address == null) throw new LXLGException(AddressException.ADDRESS_NOT_EXIST);
        if (address.getIsDelete() == 1) throw new LXLGException(AddressException.ADDRESS_IS_DELETE);
        address.setIsDelete(1);
        address.setModifiedTime(DateUtil.parseDateLong());
        updateById(address);
        return true;
    }

    @Override
    public AddressDto getUserAddressById(Integer addressId) {
        Address address = getById(addressId);
        if (address == null) throw new LXLGException(AddressException.ADDRESS_NOT_EXIST);
        return  BeanUtil.copyProperties(address, AddressDto.class);

    }
}
